package com.godofgamblers.hotelmanagementsystem.service.imp;

import com.alipay.api.domain.OrderItem;
import com.godofgamblers.hotelmanagementsystem.dto.TimeAndRoomTypeId;
import com.godofgamblers.hotelmanagementsystem.mapper.OrderItemMapper;
import com.godofgamblers.hotelmanagementsystem.mapper.OrderMapper;
import com.godofgamblers.hotelmanagementsystem.mapper.Order_ItemMapper;
import com.godofgamblers.hotelmanagementsystem.pojo.Order;
import com.godofgamblers.hotelmanagementsystem.pojo.Order_item;
import com.godofgamblers.hotelmanagementsystem.pojo.Pay_success;
import com.godofgamblers.hotelmanagementsystem.pojo.User;
import com.godofgamblers.hotelmanagementsystem.service.OrderService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author wzx
 * @date 2019/10/14 - 10:35
 */
@Service
@Transactional
@Data
public class OrderServiceImp implements OrderService {
    @Resource
    private OrderMapper orderMapper;

    @Autowired
    private Order_ItemMapper order_itemMapper;
    @Override
    //点击创建订单在订单表中插入数据
    public void insertOrder(Order order) {
        orderMapper.insertOrder(order);
    }

    @Override
    public Integer findStock(Integer room_type_id) {
        return orderMapper.findStock(room_type_id);
    }



    @Override
    public Integer findOrderByOrderNum(String order_num) {

        return orderMapper.findOrderByOrderNum(order_num);
    }

    @Override
    //传入订单号，查询对应的订单状态是多少
    public Integer findOrderState(String order_num) {
        return orderMapper.findOrderState(order_num);
    }

    @Override
    public void updateOrderState(String order_num) {
        orderMapper.updateOrderState(order_num);
    }

    @Override
    public String findRoomTypeName(Integer room_type_id) {
        return orderMapper.findRoomTypeName(room_type_id);
    }

    @Override
    public void insertDelNum(Pay_success paySuccess) {
        orderMapper.insertDelNum(paySuccess);
    }

    @Override
    public List<Order_item> findnewStock(TimeAndRoomTypeId timeAndRoomTypeId) {
        return orderMapper.findnewStock(timeAndRoomTypeId);
    }

    @Override
    public void changeOrderState(String order_num) {
        orderMapper.changeOrderState(order_num);
    }

    @Override
    public BigDecimal findUserWallet(User user) {
        return orderMapper.findUserWallet(user);
    }

    @Override
    public void updateUserWallet(User user) {
        orderMapper.updateUserWallet(user);
    }

    @Override
    public void updateStateWallet(String order_num) {
        orderMapper.updateStateWallet(order_num);
    }

    @Override
    public void updateOrderFlag(String order_num) {
        orderMapper.updateOrderFlag(order_num);
    }





    /**
     * 线上预定支付
     *
     * @param order 订单pojo类
     * @return String
     */
    @Override
    public String onlineBookingPay(Order order,Order_item order_item) {
        String result = "收款失败！";
        //设置订单状态为1(已支付)
        order.setOrder_state(1);
        //通过订单id将对应的订单状态修改为1已支付
        int updateOrderState = orderMapper.updateOrderState11(order);
        if (updateOrderState <= 0) {
            result = "订单项状态修改失败，请联系管理员！";
        }
        //修改订单项状态为2=已使用
//        order_item.setOrder_item_state(2);
//        int updataOrderItemState = order_itemMapper.updateState(order_item);
//        if(updataOrderItemState>0){
            result = "收款成功！";
//        }
        return result;
    }

}
